package lc.com.lcpicturebackend.controller;

import jakarta.annotation.Resource;
import lc.com.lcpicturebackend.annotation.AuthCheck;
import lc.com.lcpicturebackend.common.BaseResponse;
import lc.com.lcpicturebackend.common.ResultUtils;

import lc.com.lcpicturebackend.model.dto.carousel.CarouselConfigRequest;
import lc.com.lcpicturebackend.model.dto.carousel.CarouselImageUploadRequest;
import lc.com.lcpicturebackend.model.entity.CarouselItem;
import lc.com.lcpicturebackend.service.CarouselService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static lc.com.lcpicturebackend.constant.UserConstant.ADMIN_ROLE;

/**
 * 轮播图控制器
 */
@RestController
@RequestMapping("/carousel")
public class CarouselController {

    @Resource
    private CarouselService carouselService;

    /**
     * 获取轮播图配置
     */
    @GetMapping("/config")
    public BaseResponse<CarouselConfigRequest> getCarouselConfig(@RequestParam(required = false) String scene) {
        CarouselConfigRequest config = carouselService.getCarouselConfig(scene);
        return ResultUtils.success(config);
    }

    /**
     * 保存轮播图配置
     */
    @PostMapping("/config")
    @AuthCheck(mustRole = ADMIN_ROLE)
    public BaseResponse<Boolean> saveCarouselConfig(@RequestBody CarouselConfigRequest carouselConfigRequest,
                                                    @RequestParam(required = false) String scene) {
        boolean result = carouselService.saveCarouselConfig(carouselConfigRequest, scene);
        return ResultUtils.success(result);
    }

    /**
     * 获取轮播图项列表
     */
    @GetMapping("/items")
    public BaseResponse<List<CarouselItem>> listCarouselItems(@RequestParam(required = false) String scene) {
        List<CarouselItem> itemList = carouselService.listCarouselItems(scene);
        return ResultUtils.success(itemList);
    }

    /**
     * 添加轮播图项
     */
    @PostMapping("/item")
    @AuthCheck(mustRole = ADMIN_ROLE)
    public BaseResponse<CarouselItem> addCarouselItem(@RequestBody CarouselItem carouselItem) {
        CarouselItem result = carouselService.addCarouselItem(carouselItem);
        return ResultUtils.success(result);
    }

    /**
     * 更新轮播图项
     */
    @PutMapping("/item")
    @AuthCheck(mustRole = ADMIN_ROLE)
    public BaseResponse<Boolean> updateCarouselItem(@RequestBody CarouselItem carouselItem) {
        boolean result = carouselService.updateCarouselItem(carouselItem);
        return ResultUtils.success(result);
    }

    /**
     * 删除轮播图项
     */
    @DeleteMapping("/item/{id}")
    @AuthCheck(mustRole = ADMIN_ROLE)
    public BaseResponse<Boolean> deleteCarouselItem(@PathVariable Long id) {
        boolean result = carouselService.deleteCarouselItem(id);
        return ResultUtils.success(result);
    }

    /**
     * 更新轮播图项排序
     */
    @PutMapping("/items/sort")
    @AuthCheck(mustRole = ADMIN_ROLE)
    public BaseResponse<Boolean> updateCarouselItemSort(@RequestBody List<CarouselItem> items) {
        boolean result = carouselService.updateCarouselItemSort(items);
        return ResultUtils.success(result);
    }

    /**
     * 上传轮播图图片
     */
    @PostMapping("/upload")
    @AuthCheck(mustRole = ADMIN_ROLE)
    public BaseResponse<CarouselImageUploadRequest> uploadCarouselImage(@RequestParam("file") MultipartFile file) {
        CarouselImageUploadRequest result = carouselService.uploadCarouselImage(file);
        return ResultUtils.success(result);
    }
}